package com.kingsalert.kingsalert.fragment.ln_state;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.base.BaseLnFragment;
import com.kingsalert.kingsalert.bean.userinfo.OthersInfo;
import com.kingsalert.kingsalert.bean.userinfo.RpInfo;
import com.kingsalert.kingsalert.config.CountryConfig;
import com.kingsalert.kingsalert.dialog.CustomerServiceDialog;
import com.kingsalert.kingsalert.util.http.HttpUtil;

public class LnRpyFragment extends BaseLnFragment {

    private TextView totalRpCurrencyTopTv,totalRpTopTv, dueDateTopTv;
    private View detailView;
    private TextView lnAmountTv,interestTv,serviceFeeTv,ovduDaysTv,ovduPenaltyTv;
    private View vatView;
    private TextView vatTv;
    private View collapseBtn;
    private TextView collapseTv;
    private ImageView collapseIv;
    private Button payBtn;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page = "REPAY";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ln_rpy, container, false);

        view.findViewById(R.id.flr_ib_customerservice).setOnClickListener(v -> new CustomerServiceDialog(requireContext()).show());

        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.flr_refreshlayout);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            HttpUtil.getInstance().saveBuriedPoint(page,"REFRESH_CLICK","");
            HttpUtil.getInstance().requestRpDetail(requireActivity(),this::refreshDetails);
        });

        totalRpCurrencyTopTv = view.findViewById(R.id.flr_tv_totalrp_currency_top);
        totalRpTopTv = view.findViewById(R.id.flr_tv_totalrp_top);
        dueDateTopTv = view.findViewById(R.id.flr_tv_duedate_top);
        detailView = view.findViewById(R.id.flr_view_detail);
        lnAmountTv = view.findViewById(R.id.flr_tv_lnamount);
        interestTv = view.findViewById(R.id.flr_tv_interest);
        serviceFeeTv = view.findViewById(R.id.flr_tv_servicefee);
        ovduDaysTv = view.findViewById(R.id.flr_tv_ovdudays);
        ovduPenaltyTv = view.findViewById(R.id.flr_tv_ovdupenalty);
        vatView = view.findViewById(R.id.flr_view_vat);
        vatTv = view.findViewById(R.id.flr_tv_vat);
        collapseBtn = view.findViewById(R.id.flr_btn_collapse);
        collapseTv = view.findViewById(R.id.flr_tv_collapse);
        collapseIv = view.findViewById(R.id.flr_iv_copllapse);
        payBtn = view.findViewById(R.id.flr_btn_pay);

        init();

        return view;
    }

    private void init(){
        //折叠/展开
        collapseBtn.setOnClickListener(v -> collapse());

        payBtn.setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page,"SUBMIT_CLICK","");
            if (CountryConfig.getInstance().isOrderRepayH5()) HttpUtil.getInstance().orderRepayH5(requireActivity(),"NO",this::toPay);
            else if (CountryConfig.getInstance().isGetOrderRepayH5()) HttpUtil.getInstance().getOrderRepayH5(requireActivity(),"NO",this::toPay);
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        HttpUtil.getInstance().requestRpDetail(requireActivity(),this::refreshDetails);
    }

    private void collapse(){

        if (detailView.getVisibility() == View.VISIBLE){
            detailView.setVisibility(View.GONE);
            collapseTv.setText(requireContext().getString(R.string.ln_rpy_expand_details));
            collapseIv.setBackgroundResource(R.drawable.icon_check_bottom);
        }else {
            detailView.setVisibility(View.VISIBLE);
            collapseTv.setText(requireContext().getString(R.string.ln_rpy_collapse_details));
            collapseIv.setBackgroundResource(R.drawable.icon_check_top);
        }

    }

    @SuppressLint("SetTextI18n")
    public void refreshDetails(){

        String currency = RpInfo.getInstance().getCurrency();

        totalRpCurrencyTopTv.setText(currency);
        totalRpTopTv.setText(RpInfo.getInstance().getRepayAmount());
        dueDateTopTv.setText(RpInfo.getInstance().getRepayDate());
        lnAmountTv.setText(RpInfo.getInstance().getAmount()+" "+currency);
        interestTv .setText(RpInfo.getInstance().getInterest()+" "+currency);
        ovduDaysTv.setText(RpInfo.getInstance().getOverdueDay()+" "+RpInfo.getInstance().getOverdueDayUnit());
        ovduPenaltyTv.setText(RpInfo.getInstance().getOverdueAmount()+" "+currency);

        if (CountryConfig.getInstance().isCote()){
            vatView.setVisibility(View.VISIBLE);
            vatTv.setText(RpInfo.getInstance().getServiceVatFee()+" "+currency);
        }else vatView.setVisibility(View.GONE);

        if (RpInfo.getInstance().isOvdu()) ovduDaysTv.setTextColor(Color.RED);
        else ovduDaysTv.setTextColor(Color.BLACK);

        if (CountryConfig.getInstance().isFrench()) serviceFeeTv.setText(RpInfo.getInstance().getRepayProFee()+" "+currency);
        else serviceFeeTv.setText(RpInfo.getInstance().getServiceFee()+" "+currency);

    }

    public void toPay(){
        String path = OthersInfo.getInstance().getRpPath();
        if (!TextUtils.isEmpty(path)) startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(path)));
    }

}